package com.wjy.Data_Structure.graph;

import com.wjy.Data_Structure.linearlist.common.Iterator;
import com.wjy.Data_Structure.linearlist.common.LinkedList;
import com.wjy.Data_Structure.linearlist.listslinkimpl.LinkedListDLNode;

public class DirectGraph extends AbstractGraph {

	public DirectGraph() {
		super(Graph.DirectedGraph);
	}

	@Override
	public void remove(Vertex v) {
		Edge e = null;
		while (v.getOutDeg() > 0) {
			e = (Edge) v.getAdjacentEdges().first().getData();
			remove(e);
		}
		while (v.getInDeg() > 0) {
			e = (Edge) v.getReAdjacentEdges().first().getData();
			remove(e);
		}
		vertexs.remove(v.getVexPosition());

	}

	@Override
	public void remove(Edge e) {
		edges.remove(e.getEdgePosition());
		Vertex u = e.getFirstVex();
		Vertex v = e.getSecondVex();
		u.getAdjacentEdges().remove(e.getEdgeFirstPosition());
		v.getReAdjacentEdges().remove(e.getEdgeSecondPosition());
	}

	@Override
	public Edge edgeFromTo(Vertex u, Vertex v) {
		LinkedList adjEdge = u.getAdjacentEdges();
		Iterator it = adjEdge.elements();
		Edge e = null;
		for (it.first(); !it.isDone(); it.next()) {
			e = (Edge) it.currentItem();
			if (e.getSecondVex() == v)
				return e;
		}
		return null;
	}

	@Override
	public Iterator adjVertexs(Vertex u) {
		LinkedList adjVex = new LinkedListDLNode();
		LinkedList adjEdge = u.getAdjacentEdges();
		Iterator it = adjEdge.elements();
		Edge e = null;
		for (it.first(); !it.isDone(); it.next()) {
			e = (Edge) it.currentItem();
			adjVex.insertLast(e.getSecondVex());
		}
		return adjVex.elements();
	}

}
